<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_Banner{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $data = null){
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateName = new Zend_Validate();
        $validatePosition = new Zend_Validate();
        $validateImage = new Zend_Validate();
        $validateLink = new Zend_Validate();
        $validateContent = new Zend_Validate();
        
        $validateName->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 255)), true)
                        ->addValidator(new Zend_Validate_Alnum(true), true);
        
        $validatePosition->addValidator(new Zend_Validate_GreaterThan(0), true);
                  
        $validateImage->addValidator(new Zend_Validate_NotEmpty(), true);
                        
        $validateLink->addValidator(new Zend_Validate_NotEmpty(), true);
        
        $validateContent->addValidator(new Zend_Validate_NotEmpty(), true);
        
        /*
        * Check Name
        */
        
        if(!$validateName->isValid($params['name'])){
            $this->_messageError['name'] = '<label class="control-label" for="inputError">' . $translate->_('name') . current($validatename->getMessages()) . '</label>';
            $this->_arrData['name'] = '';
            $this->_classError['name'] = 'has-error';
            $this->_isError = true;
            
            if($data != null){
                $this->_arrData['name'] = $data['name'];
            }
        }else{
            $this->_messageError['name'] = '';
            $this->_arrData['name'] = $params['name'];
            $this->_classError['name'] = '';
        }
        
        /*
        * Check Position
        */
        
        if(!$validatePosition->isValid($params['position'])){
            $this->_messageError['position'] = '<label class="control-label" for="inputError">' . $translate->_('position error') . '</label>';
            $this->_arrData['position'] = '';
            $this->_classError['position'] = 'has-error';
            $this->_isError = true;
            
            if($data != null){
                $this->_arrData['position'] = $data['position'];
            }
        }else{
            $this->_messageError['position'] = '';
            $this->_arrData['position'] = $params['position'];
            $this->_classError['position'] = '';
        }
        
        /*
        * Check Image
        */
        
        if(!$validateImage->isValid($params['image'])){
            $this->_messageError['image'] = '<label class="control-label" for="inputError">' . $translate->_('image') . current($validateImage->getMessages()) . '</label>';
            $this->_arrData['image'] = '';
            $this->_classError['image'] = 'has-error';
            $this->_isError = true;
            
            if($data != null){
                $this->_arrData['image'] = $data['image'];
            }
        }else{
            $this->_messageError['image'] = '';
            $this->_arrData['image'] = $params['image'];
            $this->_classError['image'] = '';
        }
        
        /*
        * Check Link
        */
        
        if(!$validateLink->isValid($params['link'])){
            $this->_messageError['link'] = '<label class="control-label" for="inputError">' . $translate->_('link') . current($validateLink->getMessages()) . '</label>';
            $this->_arrData['link'] = '';
            $this->_classError['link'] = 'has-error';
            $this->_isError = true;
            
            if($data != null){
                $this->_arrData['link'] = $data['link'];
            }
        }else{
            $this->_messageError['link'] = '';
            $this->_arrData['link'] = $params['link'];
            $this->_classError['link'] = '';
        }
        
        $status = true;
        
        if(!isset($params['status'])){
            $status = false;
        }
        
        $this->_arrData['status'] = $status;
        
        if($data != null){
            $this->_arrData['id'] = $data['id'];
        }
        
        $date = new Zend_Date();
        $curDate = $date->get(Zend_Registry::get('date_format_insert'));
        
        if($data == null){
            $this->_arrData['publish_date'] = $curDate;
        }
        
        $pos = strpos($params['image'], 'upload/') + strlen('upload/');
        $this->_arrData['image'] = substr($params['image'], $pos);
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}